package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyConcern;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyConcernMapper;
import cn.wolfcode.trip.base.service.IStrategyConcernService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class StrategyConcernServiceImpl implements IStrategyConcernService{

    @Autowired
    private StrategyConcernMapper strategyConcernMapper;

    @Override
    public Map<String, Integer> getLikerAndCollectorNums(Long strategyId) {
        Integer likerNums  = strategyConcernMapper.selectLikerNums(strategyId);
        Integer collectorNums =  strategyConcernMapper.selectCollectorNums(strategyId);
        Map<String, Integer> map = new HashMap<>();
        map.put("likerNums",likerNums);
        map.put("collectorNums",collectorNums);
        return map;
    }


    @Override
    public Integer insertAndGetNewLikerNums(Long strategyId, Long likerId) {

        //判断是否已点过赞
        StrategyConcern likerConcern =  strategyConcernMapper.selectLikerByStrategyIdAndLikerId(strategyId,likerId);

        if (likerConcern!=null) {
            //判断改用户除了点过赞是否还收藏了
            StrategyConcern collectorConcern =  strategyConcernMapper.selectCollectorByStrategyIdAndCollectorId(strategyId,likerId);
            if (collectorConcern!=null) {
                strategyConcernMapper.updateLikerByPrimaryKey(likerConcern.getId());
            } else {
                strategyConcernMapper.deleteByPrimaryKey(likerConcern.getId());
            }
            return strategyConcernMapper.selectLikerNums(strategyId);
        }

        //将收藏的用户信息保存到strategyconcern表
        //判断用户是否已收藏
        StrategyConcern strategyConcern =  strategyConcernMapper.selectCollectorByStrategyIdAndCollectorId(strategyId,likerId);
        StrategyConcern temp = new StrategyConcern();
        User liker = new User();
        liker.setId(likerId);
        temp.setLiker(liker);
        temp.setLikeDate(new Date());
        temp.setIsViewed(StrategyConcern.ISNOTVIEWED);
        //如果strategyConcern不为空,就说明这个攻略已经被收藏,只能去修改这条记录
        if (strategyConcern != null) {
            temp.setId(strategyConcern.getId());
            System.out.println(temp);
            strategyConcernMapper.updateByPrimaryKeyIfCollectorExist(temp);
        } else {
            Strategy strategy = new Strategy();
            strategy.setId(strategyId);
            temp.setStrategy(strategy);
            strategyConcernMapper.insert(temp);
        }

        //返回点赞数
        return strategyConcernMapper.selectLikerNums(strategyId);
    }

    @Override
    public Integer insertAndGetNewCollectorNums(Long strategyId, Long collectorId) {

        //判断是否已收藏
        StrategyConcern collectorConcern =  strategyConcernMapper.selectCollectorByStrategyIdAndCollectorId(strategyId,collectorId);

        if (collectorConcern!=null) {
            //判断改用户除了收藏是否还点赞了
            StrategyConcern likerConcern =  strategyConcernMapper.selectLikerByStrategyIdAndLikerId(strategyId,collectorId);
            if (likerConcern!=null) {
                strategyConcernMapper.updateCollectorByPrimaryKey(collectorConcern.getId());
            } else {
                strategyConcernMapper.deleteByPrimaryKey(collectorConcern.getId());
            }
            return strategyConcernMapper.selectCollectorNums(strategyId);
        }


        //将点赞的用户信息保存到strategyconcern表
        StrategyConcern strategyConcern =  strategyConcernMapper.selectLikerByStrategyIdAndLikerId(strategyId,collectorId);
        StrategyConcern temp = new StrategyConcern();
        User collector = new User();
        collector.setId(collectorId);
        temp.setCollector(collector);
        temp.setCollectDate(new Date());
        //如果strategyConcern数不为空,就说明这个攻略已经被点过赞,只能去修改这条记录
        if (strategyConcern != null) {
            temp.setId(strategyConcern.getId());
            strategyConcernMapper.updateByPrimaryKeyIfLikerExist(temp);
        } else {
            Strategy strategy = new Strategy();
            strategy.setId(strategyId);
            temp.setStrategy(strategy);
            strategyConcernMapper.insert(temp);
        }

        //返回收藏数
        return strategyConcernMapper.selectCollectorNums(strategyId);


    }

}
